import { createRouter, createWebHistory } from 'vue-router'
import { userLoginStorage } from '@/stores/userloginStorage'
const router = createRouter({
  history: createWebHistory(''),
  routes:[
   
    {
      path:'/outerContainer',
      name:'outerContainer',
      requiresAuth:true,
      component: () => import('../views/OuterContainer.vue'),
      children: [
        //支付界面
        {
          path:'payMoney',
          name:'payMoney',
          requiresAuth:true,
          component: () => import('../views/PayMoney.vue')
        },
        {
          path:'payResult',
          name:'payResult',
          requiresAuth:true,
          component: () => import('../views/PayResult.vue')
        },
        {
          path:'indexHello',
          name:'indexHello',
          requiresAuth:true,
          component: () => import('../views/IndexHello.vue')
        },
        {
          //挂号
          path:'regist/order',
          name:'order',
          requiresAuth:true,
          component: () => import('../views/Order.vue')
        },
        {
          //支付
          path:'regist/payMoney',
          name:'payMoney',
          requiresAuth:true,
          component: () => import('../views/PayMoney.vue')
       },
       {
        //挂号收费 ：收费
              path:'regist/pay',
              name:'pay',
              requiresAuth:true,
              component: () => import('../views/Pay.vue')
       },
       {
        //挂号收费：费用记录查询
        path:'regist/payMemory',
        name:'payMemory',
        requiresAuth:true,
       component:()=>import('../views/PayMemory.vue')
       },{
        //挂号收费：窗口退号
        path:'regist/noOrder',
        name:"noOrder",
        requiresAuth:true,
        component:()=>import('../views/NoOrder.vue')
       },{
        //挂号收费：退号
        path:'regist/noPay',
        name:'noPay',
        requiresAuth:true,
        component:()=>import('../views/NoPay.vue')
       },
       {
        //医生工作:患者查看
        path:'doctor/lookPatient',
        name:'lookPatient',
        requiresAuth:true,
        component:()=>import('../views/LookPatient.vue')
       },
       {
        //医生工作:医生诊疗
        path:'doctor/doctorWork',
        name:'doctorWork',
        requiresAuth:true,
        component:()=>import('../views/DoctorWork.vue'),
        children: [
       {
        //医生诊疗:病案首页
         path:'bookIndex',
         name:'bookIndex',
         requiresAuth:true,
         component:()=>import('../views/BookIndex.vue'),
       },
       {//医生诊疗:检查申请
        path:'request',
        name:'request',
        requiresAuth:true,  
        component:()=>import('../views/Request.vue')
       },
       {//医生诊疗：检查结果查看
        path:'requestResult',
        name:'requestResult',
        requiresAuth:true,  
        component:()=>import('../views/RequestResult.vue')
       },
       {//医生诊疗：门诊确诊
             path:'checkOn',
             name:'checkOn',
             requiresAuth:true,
             component:()=>import('../views/CheckOn.vue')
       },
       {//医生诊疗：开诊处方
             path:'prescription',
             name:'prescription',
             requiresAuth:true,
             component:()=>import('../views/Prescription.vue')
       },
       {//医生诊疗：费用查询
        path:'fee',
        name:'fee',
        requiresAuth:true,  
        component:()=>import('../views/Fee.vue')
       }
      ]
       }, 
       //医生工作：看诊记录
       {
       path:'doctor/doctorMemory',
       name:'doctorMemory',
       requiresAuth:true,
       component:()=>import('../views/DoctorMemory.vue')
       },
       {
        //查看科室
        path:'departmentView',
        name:'departmentView',
        requiresAuth:true,
        component: () => import('../views/DepartmentView.vue')
      }
      ,{//检查管理:检查申请
        path:'checkRequestNormal',
        name:'checkRequestNormal',
        requiresAuth:true,
        component: () => import('../views/CheckRequestNormal.vue')
      },{
        //检查管理：患者录入
        path:'checkIn',
        name:'checkIn',
        requiresAuth:true,
        component: () => import('../views/CheckIn.vue')
      },{
        //检查录入
        path:'checkNormal',
        name:'checkNormal',
        requiresAuth:true,
        component: () => import('../views/CheckNormal.vue')
      } ,{
        //检查管理：检查管理
        path:'checkManage',
        name:'checkManage',
        requiresAuth:true,
        component: () => import('../views/CheckManage.vue')
      }
      ,{//检验管理：检验申请
        path:'inspectionRequest',
        name:'inspectionRequest',
        requiresAuth:true,
        component: () => import('../views/InspectionRequest.vue')
      }
      ,{//检验管理：患者录入
        path:'inspectionPatient',
        name:'inspectionPatient',
        requiresAuth:true,
        component: () => import('../views/InspectionPatient.vue')
      }
      ,{//检验管理：检验录入
        path:'inspection'
        ,name:'inspection'
        ,requiresAuth:true
        ,component: () => import('../views/Inspection.vue')
      },{
        //检验管理：检验管理
        path:'inspectionManage',
        name:'inspectionManage',
        requiresAuth:true,
        component: () => import('../views/InspectionManage.vue')
      } ,
      {//药房管理：药房发药
        path:'medicine',
        name:'medicine',
        requiresAuth:true,
        component: () => import('../views/Medicine.vue')
       
      },{//药房管理：药房退药
        path:'noMedicine',
        name:'noMedicine',
        requiresAuth:true,
        component: () => import('../views/NoMedicine.vue')
      },
      {//药房管理：药库管理
        path:'medicineManage',
        name:'medicineManage',
        requiresAuth:true,
        component: () => import('../views/MedicineManage.vue')
      },
      {//药房管理：发退药记录管理
        path:'medicineMemory',
        name:'medicineMemory',
        requiresAuth:true,
        component: () => import('../views/MedicineMemory.vue')
      },{//处置管理：处置申请
        path:'disposalRequest',
        name:'disposalRequest',
        requiresAuth:true,
        component: () => import('../views/DisposalRequest.vue')
      },{//处置管理：患者录入
        path:'disposalPatient',
        name:'disposalPatient',
        requiresAuth:true,
        component: () => import('../views/DisposalPatient.vue')
      },{//处置管理：处置录入
        path:'disposal',
        name:'disposal',
        requiresAuth:true,
        component: () => import('../views/Disposal.vue')
      },{//处置管理：处置管理
        path:'disposalManage',
        name:'disposalManage',
        requiresAuth:true,
        component: () => import('../views/DisposalManage.vue')
      }
      ] 
    },
    {
      path:'/login',
      name:'login',
      component: () => import('../views/login.vue')
    }]
});
// 定义全局守卫
router.beforeEach((to, from, next) => {
  const storage = userLoginStorage(); // 获取 store 实例
  storage.checkToken(); // 更新 token 状态
  if ((storage.token==null && to.name !== 'login')||(
    to.meta.requiresAuth && storage.isAuthenticated()
  )) {
    return next('/login'); // 如果没有认证并且不是去登录页面，则重定向
  }else if(to.name=='PayResult'){
   next();
    
  }else{
    next(); // 继续路由
  }

});

export default router